package com.ling.toolbox.util;

import android.text.TextUtils;

import java.util.List;

/**
 * 字符串帮助类
 * Created by lingquan(quan.ling@hotmail.com) on 16/7/22.
 */
public class StringUtl {
    /**
     * 转换成字符串
     * @param <T>
     */
    public interface StringProvider<T> {
        String provide(T data);
    }

    public static String build(List<String> tList, String segment) {
        return build(tList, segment, new StringProvider<String>() {
            @Override
            public String provide(String data) {
                return data;
            }
        });
    }

    /**
     * 拼接字符串,字符串之间用segment连接
     * @param tList 原始数据
     * @param segment 连接字符串
     * @param st 原始数据转成字符串转换器
     * @param <T> 原始数据类型
     * @return 拼接结果
     */
    public static <T> String build(List<T> tList, String segment, StringProvider<T> st) {
        if (st == null) {
            throw new IllegalArgumentException("请设置转换器");
        }

        if (tList == null || tList.isEmpty()) {
            return null;
        }

        StringBuilder builder = new StringBuilder();
        for (int i = 0, size = tList.size(); i < size; ++i) {
            if (i != 0 && !TextUtils.isEmpty(segment)) {
                builder.append(segment);
            }

            builder.append(st.provide(tList.get(i)));
        }

        return builder.toString();
    }
}
